package com.geovis.emergency.spd.biz.iflytek.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.geovis.emergency.spd.biz.iflytek.config.IflytekConfig;
import com.geovis.emergency.spd.biz.iflytek.model.*;
import com.geovis.emergency.spd.biz.iflytek.service.IflytekService;
import com.geovis.emergency.spd.biz.pc.service.*;
import com.geovis.emergency.spd.cache.DictCache;
import com.geovis.emergency.spd.cache.RegionCache;
import com.geovis.emergency.spd.entity.pc.entity.*;
import com.geovis.emergency.spd.entity.pc.pojo.constant.PcDisasterConstant;
import com.geovis.emergency.spd.entity.pc.pojo.enums.StatusEnum;
import com.geovis.emergency.spd.entity.pc.pojo.vo.*;
import com.geovis.emergency.spd.entity.system.entity.SystemFile;
import com.geovis.emergency.spd.entity.system.entity.SystemRegion;
import com.geovis.emergency.spd.redis.RedisUtil;
import com.geovis.emergency.spd.util.MyHttpUtil;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.io.File;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.*;
import java.util.stream.Collectors;

/**
 * <p>
 *  科大对接相关接口
 * </p>
 *
 * @author Lonsho
 * @version 1.0
 * @date 2022/8/23
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class IflytekServiceImpl implements IflytekService {


    private final IPcPlanService pcPlanService;

    private final IPcPlanDutyDeptService pcPlanDutyDeptService;

    private final IPcDrillService pcDrillService;

    private final IPcDrillDisasterEventService pcDrillDisasterEventService;
    private final IPcDrillPlanService pcDrillPlanService;
    private final IPcDrillPlanDetailService pcDrillPlanDetailService;
    /**
     * 线程池
     */
    //创建线程池
    ThreadPoolExecutor EXECUTOR_SERVICE = new ThreadPoolExecutor(2, 10, 1L,
            TimeUnit.SECONDS, new LinkedBlockingQueue<>(1024),
            Executors.defaultThreadFactory(), new ThreadPoolExecutor.AbortPolicy());
    /**
     * 文件上传接口
     * @param systemFile
     */
    @Override
    public String uploadFile(SystemFile systemFile) {
        log.warn("================ 推送科大 上传附件 START ================");
        HashMap<String, Object> paramMap = Maps.newHashMapWithExpectedSize(3);
        paramMap.put("kgId",IflytekConfig.KG_ID);
        if (IflytekConfig.IMAGE_LIST.contains(systemFile.getExtName())){
            paramMap.put("type","img");
        }else {
            paramMap.put("type","file");
        }
        File file = FileUtil.writeBytes(FileUtil.readBytes(systemFile.getDir() + systemFile.getPath()), systemFile.getName());
        paramMap.put("file", file);
        String result = MyHttpUtil.post(IflytekConfig.BASE_URL+"/commonApi/kg/dataResources/localDataUpload/v1", paramMap);
        if(StrUtil.isNotBlank(result)){
            log.warn("文件上传结果 >> {}" ,result);
            JSONObject jsonObject = JSON.parseObject(result);
            String retcode = jsonObject.getString("retcode");
            if (IflytekConfig.UPLOAD_SUCCESS_CODE.equals(retcode)){
                return jsonObject.getString("fastDfsUri");
            }
        }
        log.warn("================ 推送科大 上传附件 END ================");
        return null;
    }
    private UploadPlanFile uploadPlanFile(UploadFileTypeEnum type, SystemFile systemFile){
        String url = uploadFile(systemFile);
        if (StrUtil.isNotBlank(url)){
            HashMap<String, Object> urlMap = Maps.newHashMap();
            urlMap.put("url", Lists.newArrayList(url));
            String resultUrl = JSON.toJSONString(urlMap);
            UploadPlanFile uploadPlanFile = new UploadPlanFile();
            uploadPlanFile.setType(type);
            uploadPlanFile.setUrl(resultUrl);
            return uploadPlanFile;
        }
        return null;
    }
    private HashMap<String, Object> createParamMap(String label){
        HashMap<String, Object> paramMap = Maps.newHashMapWithExpectedSize(5);
        paramMap.put("graphName",IflytekConfig.GRAPH_NAME);
        paramMap.put("label",label);
        paramMap.put("data_type","TEXT");
        paramMap.put("cardinality","SINGLE");
        return paramMap;
    }
    /**
     * 保存预案
     * @param pcPlanDetailVO
     * @return
     */
    @Override
    public String pushPlan(PcPlanDetailVO pcPlanDetailVO) {
        if (StrUtil.isNotBlank(pcPlanDetailVO.getPushId())){
            //已经推送过了，无需再推送
            return pcPlanDetailVO.getPushId();
        }
        log.warn("================ 推送科大 保存预案 START ================");
        log.warn("预案名称：{}，预案id：{}",pcPlanDetailVO.getTitle(),pcPlanDetailVO.getId());

        HashMap<String, Object> paramMap = createParamMap(IflytekConfig.PLAN_LABEL);
        HashMap<String, Object> properties = Maps.newHashMap();
        properties.put("name",pcPlanDetailVO.getTitle());
        properties.put("title",pcPlanDetailVO.getTitle());
        properties.put("identifier",pcPlanDetailVO.getId());
        if (ObjectUtil.isNotNull(pcPlanDetailVO.getUpdateTime())){
            properties.put("updateTime",dateTimeToTimestamp(pcPlanDetailVO.getUpdateTime()));
        }else {
            properties.put("updateTime",dateTimeToTimestamp(pcPlanDetailVO.getCreateTime()));
        }
        properties.put("type",pcPlanDetailVO.getTypeName());
        properties.put("regionArea",pcPlanDetailVO.getRegionName());
        properties.put("createUser",pcPlanDetailVO.getCreateUser());

        properties.put("status", String.valueOf(StatusEnum.TRUE.getCode().equals(pcPlanDetailVO.getDataStatus())));

        //预案工作手册
        List<String> manualWorkFileList = Lists.newArrayList();
        //操作手册
        List<String> operationManualFileList = Lists.newArrayList();
        // 工作规则文件
        List<String> workRulesFileList = Lists.newArrayList();
        //预案应急文件
        List<String> planingFileList = Lists.newArrayList();

        int fileSize = pcPlanDetailVO.getManualWorkFileList().size()+
                pcPlanDetailVO.getOperationManualFileList().size()+
                pcPlanDetailVO.getWorkRulesFileList().size()+
                pcPlanDetailVO.getPlaningFileList().size();
        if (fileSize > 0){
            List<CompletableFuture<UploadPlanFile>> futureList = Lists.newArrayList();
            for (SystemFile systemFile : pcPlanDetailVO.getManualWorkFileList()) {
                CompletableFuture<UploadPlanFile> future = CompletableFuture.supplyAsync(() ->
                        uploadPlanFile(UploadFileTypeEnum.MANUAL_WORK,systemFile),EXECUTOR_SERVICE);
                futureList.add(future);
            }
            for (SystemFile systemFile : pcPlanDetailVO.getOperationManualFileList()) {
                CompletableFuture<UploadPlanFile> future = CompletableFuture.supplyAsync(() ->
                        uploadPlanFile(UploadFileTypeEnum.OPERATION_MANUAL,systemFile),EXECUTOR_SERVICE);
                futureList.add(future);
            }
            for (SystemFile systemFile : pcPlanDetailVO.getWorkRulesFileList()) {
                CompletableFuture<UploadPlanFile> future = CompletableFuture.supplyAsync(() ->
                        uploadPlanFile(UploadFileTypeEnum.WORK_RULES,systemFile),EXECUTOR_SERVICE);
                futureList.add(future);
            }
            for (SystemFile systemFile : pcPlanDetailVO.getPlaningFileList()) {
                CompletableFuture<UploadPlanFile> future = CompletableFuture.supplyAsync(() ->
                        uploadPlanFile(UploadFileTypeEnum.PLANING,systemFile),EXECUTOR_SERVICE);
                futureList.add(future);
            }
            CompletableFuture<Void> allOf = CompletableFuture.allOf(futureList.toArray(new CompletableFuture[futureList.size()]));
            //等待所有任务都完成
            try {
                allOf.get(30, TimeUnit.MINUTES);
                for (CompletableFuture<UploadPlanFile> future : futureList) {
                    UploadPlanFile uploadPlanFile = future.get(30, TimeUnit.MINUTES);
                    switch (uploadPlanFile.getType()) {
                        case MANUAL_WORK:
                            manualWorkFileList.add(uploadPlanFile.getUrl());
                            break;
                        case OPERATION_MANUAL:
                            operationManualFileList.add(uploadPlanFile.getUrl());
                            break;
                        case WORK_RULES:
                            workRulesFileList.add(uploadPlanFile.getUrl());
                            break;
                        case PLANING:
                            planingFileList.add(uploadPlanFile.getUrl());
                            break;
                        default:
                            break;
                    }
                }
            } catch (InterruptedException | TimeoutException | ExecutionException e) {
                log.error("文件上传失败 {}",e.getMessage());
            }
        }
        properties.put("manualWorkFileList",manualWorkFileList);
        properties.put("operationManualFileList",operationManualFileList);
        properties.put("workRulesFileList",workRulesFileList);
        properties.put("planingFileList",planingFileList);
        paramMap.put("properties",properties);
        log.warn("预案参数 >> {}",JSON.toJSONString(paramMap));
        String result = MyHttpUtil.post(IflytekConfig.REQUEST_GRAPH_URL, JSON.toJSONString(paramMap));

        log.warn("保存预案 >> {}", result);
        String resultId = getResultId(result);
        if (StrUtil.isNotBlank(resultId)){
            //保存推送id
            pcPlanService.update(
                Wrappers.lambdaUpdate(PcPlan.class)
                    .eq(PcPlan::getId,pcPlanDetailVO.getId())
                    .set(PcPlan::getPushId,resultId)
            );
            return resultId;
        }
        log.warn("================ 推送科大 保存预案 END ================");
        return null;
    }

    /**
     * 保存预案成员单位
     * @param dutyDeptList
     * @return
     */
    @Override
    public List<String> pushPlanDeptList(List<PcPlanDutyDeptVO> dutyDeptList){
        List<String> resulList = Lists.newArrayList();
        log.warn("================ 推送科大 保存预案成员单位 START ================");
        if (CollUtil.isNotEmpty(dutyDeptList)){
            for (PcPlanDutyDeptVO pcPlanDutyDeptVO : dutyDeptList) {
                if (StrUtil.isNotBlank(pcPlanDutyDeptVO.getPushId())){
                    resulList.add( pcPlanDutyDeptVO.getPushId());
                    continue;
                }
                HashMap<String, Object> paramMap = createParamMap(IflytekConfig.DEPT_LABEL);
                HashMap<String, Object> properties = Maps.newHashMap();
//                    properties.put("icon",Lists.newArrayList());
//                    properties.put("alias",Lists.newArrayList());
//                    properties.put("hot",null);
//                    properties.put("description","");
//                    properties.put("displayName","");
                properties.put("name",pcPlanDutyDeptVO.getDeptName());
                properties.put("identifier",pcPlanDutyDeptVO.getId());
                properties.put("duty",pcPlanDutyDeptVO.getDuty());
                properties.put("deptName",pcPlanDutyDeptVO.getDeptName());
                paramMap.put("properties",properties);

                String result =  MyHttpUtil.post(IflytekConfig.REQUEST_GRAPH_URL,JSON.toJSONString(paramMap));
                String resultId = getResultId(result);
                if (StrUtil.isNotBlank(resultId)){
                    pcPlanDutyDeptService.update(
                            Wrappers.lambdaUpdate(PcPlanDutyDept.class)
                                    .eq(PcPlanDutyDept::getId,pcPlanDutyDeptVO.getId())
                                    .set(PcPlanDutyDept::getPushId,resultId)
                    );
                    resulList.add( resultId);
                }
                ThreadUtil.sleep(100L);
            }
        }
        log.warn("================ 推送科大 保存预案成员单位 END ================");
        return resulList;
    }

    /**
     * 推送演练
     * @param pcDrill
     * @return
     */
    @Override
    public String pushDrill(PcDrill pcDrill){
        if (StrUtil.isNotBlank(pcDrill.getPushId())){
            return pcDrill.getPushId();
        }
        log.warn("================ 推送科大 保存演练 START ================");
        HashMap<String, Object> paramMap = createParamMap(IflytekConfig.DRILL_LABEL);
        HashMap<String, Object> properties = Maps.newHashMap();
        properties.put("name",pcDrill.getName());
        properties.put("identifier",pcDrill.getId());
        properties.put("drillId",pcDrill.getId());
        properties.put("drillName",pcDrill.getName());
        if (StrUtil.isNotBlank(pcDrill.getPcPlanId())){
            //演练相关的预案
            PcPlan pcPlan = pcPlanService.getById(pcDrill.getPcPlanId());
            String dictValue = DictCache.getDictValue(PcDisasterConstant.DISASTER_TYPE, pcPlan.getType());
            properties.put("drillType",dictValue);
            properties.put("drillStatus",String.valueOf(StatusEnum.TRUE.getCode().equals(pcPlan.getDataStatus())));
            SystemRegion systemRegion = RegionCache.getByCode(pcPlan.getRegionCode());
            if (ObjectUtil.isNotNull(systemRegion)) {
                properties.put("drillRegionArea",systemRegion.getName());
            }
        }
        paramMap.put("properties",properties);
        String result = MyHttpUtil.post(IflytekConfig.REQUEST_GRAPH_URL, JSON.toJSONString(paramMap));
        String resultId = getResultId(result);
        if (StrUtil.isNotBlank(resultId)){
            //保存推送id
            pcDrillService.update(
                    Wrappers.lambdaUpdate(PcDrill.class)
                            .eq(PcDrill::getId,pcDrill.getId())
                            .set(PcDrill::getPushId,resultId)
            );
            log.warn("================ 推送科大 保存演练 END ================");
            return resultId;
        }
        return null;
    }

    /**
     * 推送演练事件
     * @param eventList
     * @return
     */
    @Override
    public List<PcDrillDisasterEventVO> pushDrillDisasterEvent(List<PcDrillDisasterEventVO> eventList){
        List<PcDrillDisasterEventVO> resultList = Lists.newArrayList();
        if (CollUtil.isNotEmpty(eventList)){
            log.warn("================ 推送科大 保存演练事件 START ================");
            for (PcDrillDisasterEventVO pcDrillDisasterEvent : eventList) {
                if (StrUtil.isNotBlank(pcDrillDisasterEvent.getPushId())) {
                    resultList.add(pcDrillDisasterEvent);
                    continue;
                }
                HashMap<String, Object> paramMap = createParamMap(IflytekConfig.EVENT_LABEL);
                HashMap<String, Object> properties = Maps.newHashMap();
                properties.put("name",pcDrillDisasterEvent.getTitle());
                properties.put("identifier",pcDrillDisasterEvent.getId());
                properties.put("eventContent",pcDrillDisasterEvent.getContent());
                properties.put("level",pcDrillDisasterEvent.getLevelName());
                properties.put("typeName",pcDrillDisasterEvent.getTypeName());
                properties.put("source",pcDrillDisasterEvent.getSource());
                List<String> fileList = Lists.newArrayList();
                if (CollUtil.isNotEmpty(pcDrillDisasterEvent.getFileList())){
                    for (SystemFile systemFile : pcDrillDisasterEvent.getFileList()) {
                        UploadPlanFile uploadPlanFile = uploadPlanFile(UploadFileTypeEnum.EVENT, systemFile);
                        if (StrUtil.isNotBlank(uploadPlanFile.getUrl())){
                            fileList.add(uploadPlanFile.getUrl());
                        }
                    }
                }
                properties.put("eventFile",fileList);
                properties.put("eventTitle",pcDrillDisasterEvent.getTitle());
                properties.put("responseAction",pcDrillDisasterEvent.getResponseAction());
                paramMap.put("properties",properties);
                String result =  MyHttpUtil.post(IflytekConfig.REQUEST_GRAPH_URL,JSON.toJSONString(paramMap));
                String resultId = getResultId(result);
                if (StrUtil.isNotBlank(resultId)){
                    pcDrillDisasterEventService.update(
                            Wrappers.lambdaUpdate(PcDrillDisasterEvent.class)
                                    .eq(PcDrillDisasterEvent::getId,pcDrillDisasterEvent.getId())
                                    .set(PcDrillDisasterEvent::getPushId,resultId)
                    );
                    pcDrillDisasterEvent.setPushId(resultId);
                    resultList.add(pcDrillDisasterEvent);
                }
                ThreadUtil.safeSleep(100L);
            }
            log.warn("================ 推送科大 保存演练事件 END ================");
        }

        return resultList;
    }

    @Override
    public List<PcDrillPlanVO2> pushDrillPlan(List<PcDrillPlanVO2> drillPlanList) {
        List<PcDrillPlanVO2> resultList = Lists.newArrayList();
        if (CollUtil.isNotEmpty(drillPlanList)){
            log.warn("================ 推送科大 保存演练事件任务 START ================");
            for (PcDrillPlanVO2 pcDrillPlan : drillPlanList) {
                if (StrUtil.isNotBlank(pcDrillPlan.getPushId())) {
                    resultList.add(pcDrillPlan);
                    continue;
                }
                HashMap<String, Object> paramMap = createParamMap(IflytekConfig.TASK_LABEL);
                HashMap<String, Object> properties = Maps.newHashMap();
                properties.put("identifier",pcDrillPlan.getId());
                properties.put("dutyOrgName",pcDrillPlan.getDutyOrgName());
                properties.put("taskDuty",pcDrillPlan.getDuty());
                properties.put("name",pcDrillPlan.getContent());
                List<String> fileList = Lists.newArrayList();
                if (CollUtil.isNotEmpty(pcDrillPlan.getFileList())){
                    for (SystemFile systemFile : pcDrillPlan.getFileList()) {
                        UploadPlanFile uploadPlanFile = uploadPlanFile(UploadFileTypeEnum.EVENT, systemFile);
                        if (StrUtil.isNotBlank(uploadPlanFile.getUrl())) {
                            fileList.add(uploadPlanFile.getUrl());
                        }
                    }
                }
                properties.put("taskFile",fileList);
                paramMap.put("properties",properties);
                String result =  MyHttpUtil.post(IflytekConfig.REQUEST_GRAPH_URL,JSON.toJSONString(paramMap));
                String resultId = getResultId(result);
                if (StrUtil.isNotBlank(resultId)){
                    pcDrillPlanDetailService.update(
                            Wrappers.lambdaUpdate(PcDrillPlanDetail.class)
                                    .eq(PcDrillPlanDetail::getId,pcDrillPlan.getId())
                                    .set(PcDrillPlanDetail::getPushId,resultId)
                    );
                    pcDrillPlan.setPushId(resultId);
                    resultList.add(pcDrillPlan);
                }
                ThreadUtil.safeSleep(100L);
            }
            log.warn("================ 推送科大 保存演练事件任务 END ================");
        }
        return resultList;
    }

    /**
     * 推送演练任务反馈
     * @param taskList
     * @return
     */
    @Override
    public List<DrillPlanDetailTaskVO> pushDrillPlanTask(List<DrillPlanDetailTaskVO> taskList) {
        List<DrillPlanDetailTaskVO> resultList = Lists.newArrayList();
        if (CollUtil.isNotEmpty(taskList)) {
            log.warn("================ 推送科大 保存演练行动任务 START ================");
            for (DrillPlanDetailTaskVO drillPlanDetailTaskVO : taskList) {
                if (StrUtil.isNotBlank(drillPlanDetailTaskVO.getPushId())){
                    resultList.add(drillPlanDetailTaskVO);
                    continue;
                }
                HashMap<String, Object> paramMap = createParamMap(IflytekConfig.FEEDBACK_LABEL);
                HashMap<String, Object> properties = Maps.newHashMap();
                properties.put("name",drillPlanDetailTaskVO.getContent());
                properties.put("identifier",drillPlanDetailTaskVO.getId());
                properties.put("feedback",drillPlanDetailTaskVO.getContent());
                properties.put("principalName",drillPlanDetailTaskVO.getPrincipalName());
                properties.put("content",drillPlanDetailTaskVO.getTaskContent());
                List<String> fileList = Lists.newArrayList();
                if (CollUtil.isNotEmpty(drillPlanDetailTaskVO.getFileList())){
                    for (SystemFile systemFile : drillPlanDetailTaskVO.getFileList()) {
                        UploadPlanFile uploadPlanFile = uploadPlanFile(UploadFileTypeEnum.EVENT, systemFile);
                        if (StrUtil.isNotBlank(uploadPlanFile.getUrl())){
                            fileList.add(uploadPlanFile.getUrl());
                        }
                    }
                }
                if (CollUtil.isNotEmpty(drillPlanDetailTaskVO.getTaskStatistics())){
                    properties.put("taskStatistics",drillPlanDetailTaskVO.getTaskStatistics().stream().map(JSON::toJSONString).collect(Collectors.toList()));
                }else {
                    properties.put("taskStatistics",Lists.newArrayList());
                }
                properties.put("fileList",fileList);

                paramMap.put("properties",properties);
                String result =  MyHttpUtil.post(IflytekConfig.REQUEST_GRAPH_URL,JSON.toJSONString(paramMap));
                String resultId = getResultId(result);
                if (StrUtil.isNotBlank(resultId)){
                    pcDrillPlanDetailService.update(
                            Wrappers.lambdaUpdate(PcDrillPlanDetail.class)
                                    .eq(PcDrillPlanDetail::getId,drillPlanDetailTaskVO.getId())
                                    .set(PcDrillPlanDetail::getPushId,resultId)
                    );
                    drillPlanDetailTaskVO.setPushId(resultId);
                    resultList.add(drillPlanDetailTaskVO);
                }
                ThreadUtil.safeSleep(100L);
            }
            log.warn("================ 推送科大 保存演练行动任务 END ================");
        }
        return resultList;
    }
    /**
     * 创建关联
     * @param planId
     * @param idList
     */
    @Override
    public void pushLink(String planId,List<String> idList,String label){
        log.warn("================ 推送科大 创建关联 START ================");
        if (CollUtil.isNotEmpty(idList)){
            HashMap<String, Object> params = Maps.newHashMap();
            params.put("graphName",IflytekConfig.GRAPH_NAME);
            List<HashMap<String, Object>> edges = Lists.newArrayList();
            for (String id : idList) {
                HashMap<String, Object> edge = Maps.newHashMap();
                edge.put("source",planId);
                edge.put("target",id);
                edge.put("label",label);
                HashMap<String, Object> properties = Maps.newHashMap();
                properties.put("remark","");
                edge.put("properties",properties);
                edges.add(edge);
            }
            params.put("edges",edges);
            String result = MyHttpUtil.post(IflytekConfig.REQUEST_EDGE_URL, JSON.toJSONString(params));
            log.warn("关联结果 {}: {}",label,result);
        }
        log.warn("================ 推送科大 创建关联 END ================");
    }
    public void pushLink(List<GraphLink> linkList, String label){
        log.warn("================ 推送科大 创建关联 START ================");
        if (CollUtil.isNotEmpty(linkList)){
            HashMap<String, Object> params = Maps.newHashMap();
            params.put("graphName",IflytekConfig.GRAPH_NAME);
            List<HashMap<String, Object>> edges = Lists.newArrayList();
            for (GraphLink link : linkList) {
                HashMap<String, Object> edge = Maps.newHashMap();
                edge.put("source",link.getSource());
                edge.put("target",link.getTarget());
                edge.put("label",label);
                HashMap<String, Object> properties = Maps.newHashMap();
                properties.put("remark","");
                edge.put("properties",properties);
                edges.add(edge);
            }
            params.put("edges",edges);
            String result = MyHttpUtil.post(IflytekConfig.REQUEST_EDGE_URL, JSON.toJSONString(params));
            log.warn("关联结果 {}: {}",label,result);
        }
        log.warn("================ 推送科大 创建关联 START ================");

    }
    @Override
    public void updatePcDrill(String drillId,int count,LocalDateTime time){
        log.warn("================ 推送科大 导出方案 START ================");
        PcDrill pcDrill = pcDrillService.getById(drillId);
        if (ObjectUtil.isNotNull(pcDrill) && StrUtil.isNotBlank(pcDrill.getPushId())){
            String URL = IflytekConfig.BASE_URL+ "/graphApi/api/v1/graph/"+IflytekConfig.GRAPH_NAME+"/graph/vertexV2/"+drillId;
            HashMap<String, Object> paramMap = createParamMap(IflytekConfig.DRILL_LABEL);
            HashMap<String, Object> properties = Maps.newHashMap();
            properties.put("drillStatus","true");
            properties.put("drillCount",count);
            properties.put("drillDate",dateTimeToTimestamp(time));
            paramMap.put("properties",properties);
            log.warn("推送参数 {}",JSON.toJSONString(paramMap));
            String result = MyHttpUtil.put(URL, JSON.toJSONString(paramMap));
            log.warn("推送结果 {}",result);
        }
        log.warn("================ 推送科大 导出方案 END ================");

    }
    @Override
    public List<DrillRecommend> getDrillRecommend(String regionArea,String type){
        List<DrillRecommend> list = Lists.newArrayList();
        String URL = IflytekConfig.BASE_URL+ "/graphApi/api/v1/graph/"+IflytekConfig.GRAPH_NAME+"/graph/entity/search/v2";
        HashMap<String, Object> param = Maps.newHashMap();

        param.put("graphName",IflytekConfig.GRAPH_NAME);
        if (StrUtil.isNotBlank(regionArea)){
            param.put("regionArea",regionArea);
        }else {
            param.put("regionArea","");
        }
        if (StrUtil.isNotBlank(type)){
            param.put("type",type);
        }else {
            param.put("type","");
        }
        log.warn("请求推荐 {}",JSON.toJSONString(param));
        String result = MyHttpUtil.post(URL, param);
        if (StrUtil.isNotBlank(result)){
            log.warn("推荐结果 {}",result);
            JSONObject jsonObject = JSON.parseObject(result);
            Integer status = jsonObject.getInteger("status");
            if (IflytekConfig.SUCCESS_CODE ==  status ){
                JSONArray data = jsonObject.getJSONArray("data");
                if (ObjectUtil.isNotEmpty(data)){
                    list = data.toJavaList(DrillRecommend.class);
                }
            }
        }
        return list;
    }
    //获取关系图谱数据
    @Override
    public JSONObject getGraphData(String pushId){
        return RedisUtil.getEx("drill:graph:"+pushId, Duration.ofHours(2),()->{
            if (StrUtil.isNotBlank(pushId)){
                HashMap<String, Object> params = Maps.newHashMap();
                params.put("depth","3");
                params.put("id",pushId);
                String result = MyHttpUtil.get(IflytekConfig.GRAPH_URL, params);
                if (StrUtil.isNotBlank(result)){
                    try {
                        JSONObject jsonObject = JSON.parseObject(result);
                        Integer status = jsonObject.getInteger("status");
                        if (IflytekConfig.SUCCESS_CODE == status){
                            return jsonObject.getJSONObject("data");
                        }

                    }catch (Exception e){}
                }
            }
            return null;
        });
    }

    public String getResultId(String result){
        if (StrUtil.isNotBlank(result)){
            Res res = JSON.parseObject(result, Res.class);
            if (IflytekConfig.SUCCESS_CODE == res.getStatus()){
                log.warn("推送成功 {}",result);
                if (null != res.getData() && CollUtil.isNotEmpty(res.getData().getVertices())){
                   return res.getData().getVertices().get(0).getId();
                }
            }else {
                log.error("请求失败,status：{} ,msg：{}",res.getStatus(), res.getMessage());
            }
        }
        return null;
    }
    private  long dateTimeToTimestamp(LocalDateTime ldt){
        return ldt.toInstant(ZoneOffset.of("+8")).toEpochMilli();
    }
}
